/**
 * 
 */
import { mapGetters } from 'vuex'

export default {
  watch: {
    /**
     * 监听场景当前时间，更新地球上渲染物体的数据
     * @author Farley
     * @date 2021-11-24
     * @param {any} time
     * @returns {any}
     */
    currentTime(time) {
      let currentTime = DT.Cesium.JulianDate.fromIso8601(time)
      if (this.nextQuery.nextTime) {
        let nextQueryTime = DT.Cesium.JulianDate.fromIso8601(this.nextQuery.nextTime)
        let nextPreTime = DT.Cesium.JulianDate.fromIso8601(this.nextQuery.preTime)
        // 监听场景当前时间小于下一次请求时间时，并且分钟差在 5 分钟（300秒）之内，发送请求下一时段数据
        if ((DT.Cesium.JulianDate.lessThan(currentTime, nextQueryTime) && (DT.Cesium.JulianDate.secondsDifference(nextQueryTime, currentTime) < 10 * this.speed))) {
          if (!this.nextQuery.isQeury) {
            if (this.nextQuery.preTime !== this.nextQuery.nextTime) {
              this.nextQuery.isQeury = true
              // 请求时间
              let queryTime = moment(this.nextQuery.nextTime).subtract(300, 'seconds').utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z'
              this.nextQuery.preTime = this.nextQuery.nextTime
              this.getMap({ sceneId: this.sceneId, startTime: queryTime, step: 60 * 1 }, () => {
                this.nextQuery.isQeury = false
              })
            }
          }
        }
        // 如果当前时间小于preTime 或者大于nextQueryTime 则需要更新数据
        if (DT.Cesium.JulianDate.greaterThan(currentTime, nextQueryTime) || (DT.Cesium.JulianDate.lessThan(currentTime, nextPreTime) && (DT.Cesium.JulianDate.secondsDifference(nextPreTime, currentTime) > 10 * this.speed))) {
          if (!this.nextQuery.isQeury) {
            this.nextQuery.isQeury = true
            // 请求时间
            this.nextQuery.preTime = time
            this.getMap({ sceneId: this.sceneId, startTime: time, step: 60 * 1 }, () => {
              this.nextQuery.isQeury = false
            })
          }
        }
      }
    },
  },
  computed: {
    ...mapGetters(['currentTime']),
  },
}